package com.ruoyi.spspublic.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.spspublic.domain.TMisBlob;
import com.ruoyi.spspublic.domain.dto.PartymanFlowFileDto;
import com.ruoyi.spspublic.domain.dto.TMisBlobDownloadDTO;
import com.ruoyi.spspublic.service.ITMisBlobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 业务附件Controller
 *
 * @author wangwei
 * @date 2023-06-19
 */
@RestController
@RequestMapping("/spspublic/fileblob")
public class TMisBlobController extends BaseController {
  @Autowired private ITMisBlobService tMisBlobService;

  /** 查询业务附件列表 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:list')")
  @GetMapping("/list")
  public TableDataInfo list(TMisBlob tMisBlob) {
    startPage();
    List<TMisBlob> list = tMisBlobService.selectTMisBlobList(tMisBlob);
    for (TMisBlob misBlob : list) {
      if (!misBlob.getCtType().equals("03")) misBlob.setCtFile("");
    }
    return getDataTable(list);
  }

  /** 查询业务附件列表 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:list')")
  @GetMapping("/generateToken")
  public AjaxResult generateToken(TMisBlob tMisBlob) {
    List<TMisBlob> list = tMisBlobService.selectTMisBlobList(tMisBlob);
    String uuid = UUID.randomUUID().toString();
    tMisBlobService.generateDownloadToken(uuid, list);
    return AjaxResult.success(uuid);
  }

  /** 导出业务附件列表 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:export')")
  //    @Log(title = "业务附件", businessType = BusinessType.EXPORT)
  //    @GetMapping("/export")
  //    public AjaxResult export(TMisBlob tMisBlob)
  //    {
  //        List<TMisBlob> list = tMisBlobService.selectTMisBlobList(tMisBlob);
  //        ExcelUtil<TMisBlob> util = new ExcelUtil<TMisBlob>(TMisBlob.class);
  //        return util.exportExcel(list, "fileblob");
  //    }

  /** 获取业务附件详细信息 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:query')")
  @GetMapping(value = "/{ctId}")
  public AjaxResult getInfo(@PathVariable("ctId") String ctId) {
    return AjaxResult.success(tMisBlobService.selectTMisBlobById(ctId));
  }

  /** 新增业务附件 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:add')")
  @Log(title = "业务附件", businessType = BusinessType.INSERT)
  @PostMapping
/*
  @SaveDataScope
*/
  public AjaxResult add(@RequestBody TMisBlob tMisBlob) {
    return toAjax(tMisBlobService.insertTMisBlob(tMisBlob));
  }

  /** 修改业务附件 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:edit')")
  @Log(title = "业务附件", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
/*
  @SaveDataScope
*/
  public AjaxResult edit(@RequestBody TMisBlob tMisBlob) {
    return toAjax(tMisBlobService.updateTMisBlob(tMisBlob));
  }

  /** 删除业务附件 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:remove')")
  @Log(title = "业务附件", businessType = BusinessType.DELETE)
  @PostMapping("/delete/{ctIds}")
  public AjaxResult remove(@PathVariable String[] ctIds) {
    return toAjax(tMisBlobService.deleteTMisBlobByIds(ctIds));
  }

  /** 删除业务附件 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:remove')")
  @Log(title = "业务附件", businessType = BusinessType.DELETE)
  @PostMapping("/removeCtIdp")
  public AjaxResult removeCtIdp(String ctIdp) {
    return toAjax(tMisBlobService.deleteTMisBlobByIdPs(ctIdp));
  }

  /** 批量上传 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:upload')")
  //    @Log(title = "批量上传", businessType = BusinessType.IMPORT)
  @PostMapping("/batchUpload")
  public AjaxResult batchUpload(
      TMisBlob tMisBlob, MultipartFile multipartFiles, MultipartFile file, String MD5) {
    return tMisBlobService.batchUpload(tMisBlob, file);
  }

  /** 下载文件 */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:download')")
  @GetMapping("/download")
  public AjaxResult download(TMisBlobDownloadDTO tMisBlob, HttpServletResponse response) {
    return tMisBlobService.download(tMisBlob, response);
  }

  /**
   * 文件预览路径加密
   *
   * @param tMisBlob
   * @return
   */
  //    @PreAuthorize("@ss.hasPermi('spspublic:fileblob:preview')")
  @GetMapping("/preview")
  public AjaxResult handlePreview(TMisBlobDownloadDTO tMisBlob) {
    return tMisBlobService.handlePreview(tMisBlob);
  }

  @PostMapping("/addExternalLink")
  public AjaxResult addExternalLink(TMisBlob tMisBlob) {
    // todo
    return AjaxResult.success(tMisBlobService.insertTMisBlob(tMisBlob));
  }

  @GetMapping("/checkMD5")
  public boolean checkMD5(String MD5) {
    return true;
  }

  /** 上传图片 */
  @PostMapping("/uploadFileImage")
  public AjaxResult uploadFileImage(@RequestParam("file") MultipartFile file, String funno)
      throws IOException {
    return tMisBlobService.uploadFileImage(file, funno);
  }

  /**
   * 党员发展流程附件
   *
   * @param
   * @return
   */
  @GetMapping("/partymanFlowFileList")
  public TableDataInfo partymanFlowFileList(PartymanFlowFileDto partymanFlowFileDto) {
    List<PartymanFlowFileDto> list = tMisBlobService.partymanFlowFileList(partymanFlowFileDto);
    return getDataTable(list);
  }
}
